package com.ald.crossex.action.wechat;

import com.ald.crossex.dto.wechat.OrderRequest;
import com.ald.crossex.dto.wechat.OrderResponse;
import com.ald.crossex.entity.TCeCoreUser;
import com.ald.crossex.entity.TCeCoreWechat;
import com.ald.crossex.service.OrderService;
import com.ald.crossex.service.UserService;
import com.ald.crossex.service.wechat.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lishijiong on 15/3/9.
 */
@Controller
@RequestMapping(value = "/")
public class OrdersController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WechatService wechatService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "order")
    public ModelAndView redirectOrder(@RequestParam(value = "code") String code, @RequestParam(value = "state") String state, HttpServletRequest request) {

        TCeCoreWechat wechat = webAuthorize(wechatService, code, state);
        ModelAndView mav = new ModelAndView("order");
        if(wechat!=null){
            mav.addObject("wid",wechat.getWid());
        }
        TCeCoreUser user = userService.getUserByWechatId(wechat.getWid());
        if(user != null){
            mav.addObject("uid",user.getUid());
        }
        generateJsSDKConfig(request, mav, code, state);
        return mav;
    }


    /**
     * 未完成的订单
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "order/unfinish")
    @ResponseBody
    public OrderResponse unfinishOrder(@RequestBody OrderRequest request) {

        OrderResponse response = new OrderResponse();
        response.setPage(orderService.myUnfinishOrder(request.getUid()));

        return response;


    }

    @RequestMapping(value = "order/finished")
    @ResponseBody
    public OrderResponse finishedOrder(@RequestBody OrderRequest request) {

        OrderResponse response = new OrderResponse();
        response.setPage(orderService.myFinishedOrder(request.getUid()));


        return response;


    }


}
